package ca.inrs.ete.app.shopx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.GridView;
import ca.inrs.ete.app.shopx.util.Constants;

public class SHOPFilesView extends FragmentActivity {

	private static final String PREFS     = "SHOPFilesView.pref";
	private static final String PREFS_URI = "SHOPFilesView.pref.URI";

	private Uri mSrcUri = null;
	private GridView mGridView = null;
	private SHOPFilesViewAdapter mAdapter = null;

	private BroadcastReceiver syncReceiver = new BroadcastReceiver () {
		@Override
		public void onReceive(Context context, Intent intent) {
			final Boolean sucess = intent.getBooleanExtra(Constants.MSG_SYNC_KEY_SUCCESS, false);
			if (! sucess) return;
			setProgressBarVisibility(false);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		requestWindowFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.activity_view_files);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
	    mAdapter  = new SHOPFilesViewAdapter(this);
		mGridView = (GridView) findViewById(R.id.filesList);
        mGridView.setOnItemClickListener(mAdapter);

        registerReceiver(syncReceiver, new IntentFilter(Constants.MSG_SYNC_TERMINATED));
	}

	@Override
	protected void onStart() {
		super.onStart();

		// ---  Retrieve source uri
		mSrcUri = getIntent().getData();
		if (mSrcUri == null) {
			String pname = String.format("%s@%08x", PREFS, android.os.Process.myTid());
			SharedPreferences prefs = getSharedPreferences(pname, Context.MODE_PRIVATE);
			mSrcUri = Uri.parse( prefs.getString(PREFS_URI, "") );
		}
		
		// ---  Query images files
		Cursor c = getContentResolver().query(mSrcUri, null, null, null, null);
		Uri[] images = new Uri[c.getCount()];
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); ++i) {
			images[i] = Uri.parse(c.getString(2));
			c.moveToNext();
		}
		
		// ---  Set title
		String date = mSrcUri.getQuery();
		date = date.substring( date.indexOf("=")+1 );
		date = date.substring( date.lastIndexOf("/")+1 );
		setTitle(String.format("%s - %s", getString(R.string.title_activity_files), date));

		// ---  Adapter
		mAdapter.setImages(images);
	    mGridView.setAdapter(mAdapter);
	}

	@Override
	protected void onStop() {
		super.onPause();
		String pname = String.format("%s@%08x", PREFS, android.os.Process.myTid());
		SharedPreferences prefs = getSharedPreferences(pname, Context.MODE_PRIVATE);
		prefs.edit().putString(PREFS_URI, mSrcUri.toString())
		            .commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
            return true;
		case R.id.menu_sync:      MainMenu.onMenuSync(this); 	 return true;
		case R.id.menu_settings:  MainMenu.onMenuSettings(this); return true;
		case R.id.menu_help:      MainMenu.onMenuHelp(this);     return true;
		case R.id.menu_about:     MainMenu.onMenuAbout(this);    return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * @return the mSrcUri
	 */
	public Uri getSrcUri() {
		return mSrcUri;
	}

}


